//
//  PCCircle.m
//  PCCirle
//
//  Created by 任行亚 on 15/10/21.
//  Copyright © 2015年 BLUEMOBI. All rights reserved.
//

#import "PCCircle.h"
//#import 

@interface PCCircle ()
{
    CGFloat radius;
    NSDecimalNumber *_startRate;
    
}
@property(nonatomic) CGPoint CGPoinCerter;
@property(nonatomic) CGFloat endAngle;
@property(nonatomic) BOOL clockwise;
@property (nonatomic, strong) CADisplayLink *displayLink;
@end

@implementation PCCircle
//
//
//
- (instancetype) initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if(self){
        self.lineWith = 10.0;
        self.fillColor = [UIColor greenColor];
        self.unfillColor = [UIColor lightGrayColor];
        self.clockwise = YES;
        self.backgroundColor = [UIColor clearColor];
        self.percentage = 0.5;
        self.startAngle = 180;
        self.endAngle = 360;
        
    }
    return self;
}
#pragma mark setMethod
/**
 *  画图函数
 *
 *  @param rect rect description
 */
-(void)drawRect:(CGRect)rect{
    [self initData];
    [self drawMiddlecircle];
    [self drawOutCCircle];
    
    //画外围大圆
    [self drawMiddlecirclebig];
    [self drawOutCCirclebig];
    
    
}
/**
 *  参数设置
 */
-(void)initData{
    //中心标签设置
    CGFloat center =MIN(self.bounds.size.height/2, self.bounds.size.width/2);
    self.CGPoinCerter = CGPointMake(center, center);
    self.centerLable = [[UILabel alloc]initWithFrame:CGRectMake(0, -25, 2*center, 2*center)];
    self.centerLable.textAlignment=NSTextAlignmentCenter;
    self.centerLable.backgroundColor=[UIColor clearColor];
    self.centerLable.adjustsFontSizeToFitWidth = YES;
    self.centerLable.textColor=[UIColor whiteColor];
    self.centerLable.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
    self.contentMode = UIViewContentModeRedraw;
    [self addSubview: self.centerLable];
    
    
    //分界线
    UIView *fenjiexian=[[UIView alloc]initWithFrame:CGRectMake(center-40, center-10, 80, 1)];
    fenjiexian.backgroundColor=[UIColor whiteColor];
    [self addSubview:fenjiexian];
    
    //订单成交率
    UILabel *chengjiaolv=[[UILabel alloc]initWithFrame:CGRectMake(center-40, center-5, 80, 20)];
    chengjiaolv.text=@"订单成交率";
    chengjiaolv.textColor=[UIColor whiteColor];
    chengjiaolv.textAlignment=NSTextAlignmentCenter;
    chengjiaolv.font=[UIFont systemFontOfSize:15];
    [self addSubview:chengjiaolv];
    
    
    if(self.percentage>1){
        self.percentage = 1.0;
    }
    else if(self.percentage < 0){
        self.percentage = 0;
    }
    //半径计算
    radius = MIN(self.bounds.size.height/2-self.lineWith/2, self.bounds.size.width/2-self.lineWith/2);
    //起点与终点坐标
    self.endInnerAngle = DEGREES_TO_RADIANS(self.endInnerAngle);
    self.startAngle = DEGREES_TO_RADIANS(self.startAngle);
    self.endAngle = self.percentage*(self.endInnerAngle - self.startAngle) + self.startAngle;
    
    _startRate = [[NSDecimalNumber alloc] initWithString:@"0"];
    
    
    // 配置CADisplayLink
    [self configDisplayLink];
    
}
/**
 *  显示圆环
 */
-(void )drawOutCCircle{
    UIBezierPath *bPath = [UIBezierPath bezierPathWithArcCenter:self.CGPoinCerter radius:radius-10 startAngle:self.startAngle endAngle:self.endAngle clockwise:self.clockwise];
    bPath.lineWidth=self.lineWith;
    bPath.lineCapStyle = kCGLineCapSquare;
    bPath.lineJoinStyle = kCGLineCapSquare;
    [self.fillColor setStroke];
    [bPath stroke];
}
/**
 *  辅助圆环
 */
-(void)drawMiddlecircle{
    UIBezierPath *cPath = [UIBezierPath bezierPathWithArcCenter:self.CGPoinCerter radius:radius-10 startAngle:self.startAngle endAngle:self.endInnerAngle clockwise:self.clockwise];
    cPath.lineWidth=self.lineWith;
    cPath.lineCapStyle = kCGLineCapSquare;
    cPath.lineJoinStyle = kCGLineCapSquare;
    UIColor *color = self.unfillColor;
    [color setStroke];
    [cPath stroke];
    
    
}

//画大圆
/**
 *  显示圆环
 */
-(void )drawOutCCirclebig{
    
    UIBezierPath *bPath = [UIBezierPath bezierPathWithArcCenter:self.CGPoinCerter radius:radius startAngle:self.startAngle endAngle:self.endAngle clockwise:self.clockwise];
    bPath.lineWidth=3;
    bPath.lineCapStyle = kCGLineCapRound;
    bPath.lineJoinStyle = kCGLineJoinRound;
    [self.fillColor setStroke];
    [bPath stroke];
}
/**
 *  辅助圆环
 */
-(void)drawMiddlecirclebig{
    UIBezierPath *cPath = [UIBezierPath bezierPathWithArcCenter:self.CGPoinCerter radius:radius startAngle:self.startAngle endAngle:self.endInnerAngle clockwise:self.clockwise];
    cPath.lineWidth=3;
    cPath.lineCapStyle = kCGLineCapRound;
    cPath.lineJoinStyle = kCGLineJoinRound;
    UIColor *color = self.unfillColor;
    [color setStroke];
    [cPath stroke];
}

#pragma mark 配置CADisplayLink
- (void)configDisplayLink {
    _displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(drawCircle)];
    [_displayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
//    _displayLink.frameInterval =1;
//    _displayLink.paused = YES; // 默认暂停
}

#pragma mark - event response

- (void)drawCircle {
    
    NSDecimalNumber *everyON=[[NSDecimalNumber alloc]initWithString:[NSString stringWithFormat:@"%f",self.percentage]];
    
    NSDecimalNumber *jianfa = [everyON decimalNumberBySubtracting:_startRate];
    
    NSString *string=[NSString stringWithFormat:@"%@",jianfa];
    
    if([string rangeOfString:@"-"].location !=NSNotFound)//_roaldSearchText
    {
        _displayLink.paused = YES;
        
        NSDecimalNumber *yibai=[[NSDecimalNumber alloc]initWithString:@"100"];
        
        NSDecimalNumber *jieguo=[everyON decimalNumberByMultiplyingBy:yibai];

        self.centerLable.text = [NSString stringWithFormat:@"%@%%",jieguo];
        
        return;
    }
    
//    if ([string isEqualToString:@"0"]) {
//        _displayLink.paused = YES;
//        return;
//    }
    
    NSDecimalNumber *xiaoshu=[[NSDecimalNumber alloc]initWithString:@"0.01"];
    
    _startRate =[_startRate decimalNumberByAdding:xiaoshu];
    self.centerLable.font = [UIFont systemFontOfSize:radius/3];
    
    
    
    NSDecimalNumber *yibai=[[NSDecimalNumber alloc]initWithString:@"100"];
    
    NSDecimalNumber *jieguo=[_startRate decimalNumberByMultiplyingBy:yibai];
    
    self.centerLable.text = [NSString stringWithFormat:@"%@%%",jieguo];
    
//    _startAngle += 3.6;
}


@end
